import React, { useState, useEffect } from 'react'
import { backgroundObj, navigateTo } from '@/utils/utils'
import { View, Swiper, SwiperItem, Button, Input, Text, Textarea } from '@tarojs/components';
import FixedBtn from "@/components/FormCondition/FixedBtn";
import Upload from "@/components/FormCondition/UploadImg";
import Taro, { getCurrentInstance, } from '@tarojs/taro'
import { fakeSaveHelpQaAnswer, fakeEditAnswer } from '@/server/shop'
import { toast, goBack } from '@/utils/utils'
import './index.scss'
const MutualQuestion = (props) => {
    const { id, title, type, text, defaultList = '' } = getCurrentInstance().router.params
    const [params, setParams] = useState({
        list: [],
        remark: ''
    })
    useEffect(() => {
        if (type !== 'install') {
            setParams({
                remark: text,
                list: defaultList.length > 0 ? JSON.parse(defaultList) : []
            })
        }
    }, [])
    const {
        list,
        remark
    } = params
    const submit = () => {
        if (!remark) {
            return toast('请填写问题')
        }
        else {
            if (type === 'install') {
                fakeSaveHelpQaAnswer({
                    content: JSON.stringify({
                        text: remark,
                        images: list.toString(),


                    }),
                    postId: id
                }).then(val => {
                    goBack()
                })
            } else {
                fakeEditAnswer({
                    id: id,
                    content: JSON.stringify(
                        {
                            text: remark,
                            images: list.toString()
                        }
                    )
                }).then(val => {
                    goBack()
                })
            }

        }
    }
    return <View className='MutualQuestion_box'>
        <View className='MutualQuestion_title_box'>
            <Text className='MutualQuestion_title_icon'></Text>
            {title}
        </View>
        <View className="MutualQuestion_textArea">
            <View className='MutualQuestion_textArea_changeBox'>
                <Textarea value={remark} onInput={(e) => {
                    setParams({
                        ...params,
                        remark: e.target.value
                    })
                }} placeholder='请填写10字以上的问题描述，有助于帮您快速解决' className='MutualQuestion_textArea_box'>

                </Textarea>
                <View className="MutualQuestion_uploadImage">
                    <Upload data={list} onChange={(e) => {
                        setParams({
                            ...params,
                            list: e
                        })
                    }}></Upload>
                </View>
            </View>

        </View>
        <FixedBtn onClick={(e) => {
            submit()
        }} btnText={'发布回答'}></FixedBtn>
    </View >
}
export default MutualQuestion
